package cn.itcast.controller;


import cn.itcast.constant.MessageConstant;
import cn.itcast.entity.PageResult;
import cn.itcast.entity.QueryPageBean;
import cn.itcast.entity.Result;
import cn.itcast.pojo.Role;
import cn.itcast.service.RoleService;
import com.alibaba.dubbo.config.annotation.Reference;
import lombok.extern.slf4j.Slf4j;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("role")
@Slf4j
public class RoleController {

    @Reference
    private RoleService roleService;


    /**
     * 查询角色管理列表
     * @param queryPageBean 查询条件
     * @return
     */
    @GetMapping("page")
    public Result findPage(QueryPageBean queryPageBean){
        try {
            PageResult pageResult = roleService.findPage(queryPageBean);
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, pageResult);
        } catch (Exception e) {
            log.error("查询角色管理列表失败", e);
        }
        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }


    /**
     * 新增角色
     * @param permissionIds 权限id集合
     * @param menuIds 菜单id集合
     * @param role 角色基本信息
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_ADD')")
    @PostMapping
    public Result handleAdd(int[] permissionIds, int[] menuIds, @RequestBody Role role){
        System.out.println("role = " + role);
        try {
            Boolean flag = roleService.handleAdd(permissionIds, menuIds, role);
            if (flag){
                return new Result(true, MessageConstant.ADD_ROLE_SUCCESS, flag);
            }
        } catch (Exception e) {
            log.error("新增角色失败", e);
        }
        return new Result(false, MessageConstant.ADD_ROLE_FAIL);
    }


    /**
     *通过id查询角色信息
     * @param id 角色id
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @GetMapping("{id}")
    public Result findById(@PathVariable("id") Integer id){
        try {
            Role role = roleService.findById(id);
            if (role != null){
                return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, role);
            }
        } catch (Exception e) {
            log.error("通过id查询角色信息失败", e);
        }
        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }


    /**
     *通过角色id查询角色菜单id集合
     * @param id 角色id
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @GetMapping("{id}/menu")
    public Result findMenuById(@PathVariable("id") Integer id){
        try {
            List<Integer> list = roleService.findMenuById(id);
            if (list != null && list.size() > 0){
                return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, list);
            }
        } catch (Exception e) {
            log.error("通过角色id查询角色菜单id集合失败", e);
        }
        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }



    /**
     *通过角色id查询角色权限id集合
     * @param id 角色id
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @GetMapping("{id}/permission")
    public Result findPermissionById(@PathVariable("id") Integer id){
        try {
            List<Integer> list = roleService.findPermissionById(id);
            if (list != null && list.size() > 0){
                return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, list);
            }
        } catch (Exception e) {
            log.error("通过角色id查询角色权限id集合失败", e);
        }
        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }



    /**
     * 编辑角色
     * @param permissionIds 权限id集合
     * @param menuIds 菜单id集合
     * @param role 角色基本信息
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_EDIT')")
    @PutMapping()
    public Result handleEdit(int[] permissionIds, int[] menuIds, @RequestBody Role role){
        try {
            Boolean flag = roleService.handleEdit(permissionIds, menuIds, role);
            if (flag){
                return new Result(true, MessageConstant.EDIT_ROLE_SUCCESS, flag);
            }
        } catch (Exception e) {
            log.error("编辑角色失败", e);
        }
        return new Result(false, MessageConstant.EDIT_ROLE_FAIL);
    }


    /**
     * 根据id删除角色
     * @param id  角色id
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_DELETE')")
    @DeleteMapping("{id}")
    public Result handleDelete(@PathVariable("id") Integer id){
        try {
            Boolean flag = roleService.handleDelete(id);
            if (flag){
                return new Result(true, MessageConstant.DELETE_ROLE_SUCCESS, flag);
            }
        } catch (Exception e) {
            log.error("根据id删除菜单失败", e);
        }
        return new Result(false, MessageConstant.DELETE_ROLE_FAIL);
    }



    /**
     * 查询所有角色
     * @return
     */
    @PreAuthorize("hasAuthority('ROLE_QUERY')")
    @GetMapping("all")
    public Result findAllRole(){
        try {
            List<Role> list = roleService.findAllRole();
            return new Result(true, MessageConstant.QUERY_ROLE_SUCCESS, list);
        } catch (Exception e) {
            log.error("询所有角色失败", e);
        }
        return new Result(false, MessageConstant.QUERY_ROLE_FAIL);
    }

}
